/**
 * Copyright 2011 Alex Vikulov (vikuloff@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.vvip.stockchart.util;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.RectF;


public class GridPainter 
{	
	public enum GridType
	{
		HORIZONTAL,
		VERTICAL
	}
	
	public enum GridLabelPosition
	{
		NEAR,
		FAR,
	}	
	
	public static void paint(RectF clipRect,Canvas c,GridPainter.GridType type, 
			int linesCount,Paint paint, IGridLabelsProvider labelsProvider, GridLabelPosition labelPos,float labelLength)
	{
		if(linesCount <= 0) return;
		
		final Rect bounds = new Rect();
		
		boolean isNear = (labelPos == GridLabelPosition.NEAR);
		
		if(type == GridType.HORIZONTAL)
		{			
			float total = (clipRect.bottom-clipRect.top);
			float f = total/(float)linesCount;
			
			paint.setTextAlign(isNear?Align.LEFT:Align.RIGHT);
						
			for(float y = clipRect.top;y<clipRect.bottom;y+=f)
			{							
				if(null != labelsProvider)
				{
					float x = (isNear?clipRect.left:clipRect.right);
					float dx = (isNear?labelLength:-labelLength);
					
					c.drawLine(x, y, x+dx, y, paint);
					
					String label = labelsProvider.getLabel(y, total);
					
					if(null != label)
					{						
						
						paint.getTextBounds(label, 0, label.length(), bounds);
						
						c.drawText(label, x+dx, y+(float)(bounds.bottom-bounds.top)/2.0f, paint);
					}
				}
				else
				{
					c.drawLine(clipRect.left, y, clipRect.right, y, paint);
				}
			}		
		}
		
		else if(type == GridType.VERTICAL)
		{
			float total =(clipRect.right-clipRect.left);
			float f = total/(float)linesCount;
			
			paint.setTextAlign(Align.CENTER);
			
			for(float x = clipRect.left;x<clipRect.right;x+=f)
			{										
				if(null != labelsProvider)
				{
					String label = labelsProvider.getLabel(x, total);
					
					float y = (isNear?clipRect.top:clipRect.bottom);
					float dy = (isNear?labelLength:-labelLength);
					
					c.drawLine(x, y, x, y+dy, paint);
					
					if(null != label)
					{
						
						paint.getTextBounds(label, 0, label.length(), bounds);
						float textHeight = bounds.bottom - bounds.top;								
						c.drawText(label, x, y+dy+(isNear?textHeight+1:-textHeight/2.0f), paint);
					}
				}
				else
				{
					c.drawLine(x, clipRect.top, x, clipRect.bottom, paint);	
				}
			}	
		}		
	}
	
	public static void paint(RectF clipRect,Canvas c,GridPainter.GridType type, int linesCount,Paint paint)
	{
		paint(clipRect,c,type, linesCount,paint,null,null,0.0f);
	}
}
